package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.TimeUtil;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdSoftwareStaDao;
import com.ccp.dev.qbdms.model.WQbdSoftwareSta;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import com.ccp.dev.system.model.SysOrg;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 软件静态度量 信息操作服务类
 *
 * @author 于金
 * @date 2020-07-06 09:53:26
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdSoftwareStaService extends BaseService<WQbdSoftwareSta> {

    @Resource
    private WQbdSoftwareStaDao dao;

    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
    List<WQbdSoftwareSta> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WQbdSoftwareSta data : dataList) {
        excel.cell(startRow, 0).value(data.getId());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

    public List<WQbdSoftwareSta> reportList(String id, String tableName) {
        return dao.reportList(id,tableName);
    }

    /**
     * 导入新增
     * @param list  数据集
     * @throws Exception 异常
     */
    public void addImportData(List<WQbdSoftwareSta> list) {
        BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel();
        list.forEach(a->{
            //二级
            if(BaseUtil.SystemLevel.HOSPITALLEVEL.equals(currentSystemLevel)){
                a.setReportStatus(ZhiLiangConst.REPORT_STATUS_GROUPTOBEREPORTED);
            }
            //三级
            if(BaseUtil.SystemLevel.PLANTLEVEL.equals(currentSystemLevel)){
                a.setReportStatus(ZhiLiangConst.REPORT_STATUS_TEMP_TOBEREPORTED);
            }
            a.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath());
            //新增操作
            //新增数据前生成随机Id
            a.setId(UUIDUtils.getUUIDFor32());
            a.setTenantId(ContextUtil.getCurrentUserTenantId());
            a.setCreateBy(ContextUtil.getCurrentUserId());
            a.setCreateTime(new Date());
            //通用
            a.setFilldept(ContextUtil.getCurrentOrg().getOrgName());
            a.setFilldate(TimeUtil.getCurrentDate());
            a.setFillman(ContextUtil.getCurrentUser().getFullName());
            a.setFillmanaccount(ContextUtil.getCurrentUser().getAccount());
            a.setFilldeptid(((SysOrg) ContextUtil.getCurrentOrg()).getCode());
        });
        if(!list.isEmpty()){
            dao.addList(list);
        }
    }

    public List<WQbdSoftwareSta> queryAll(QueryFilter queryFilter) {
        return dao.queryAll(queryFilter);
    }

    /**
     * 保存接口传输的数据
     *
     * @param wQbdSoftwareSta 主表model,有子表的包含子表数据
     * @return
     */
    public int saveAll(List<WQbdSoftwareSta> wQbdSoftwareSta,List<ExtendSysFile> extendSysFileList){
        ReadAttachmentUtil.getEntityData(wQbdSoftwareSta,extendSysFileList);
        List<WQbdSoftwareSta> wQbdSoftwareStas = new ArrayList<>();
        for(WQbdSoftwareSta softwareSta : wQbdSoftwareSta){
            if(StringUtil.isEmpty(softwareSta.getFilldeptHospitalId())){
                softwareSta.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(softwareSta.getFilldeptid()));
            }else{
                softwareSta.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(softwareSta.getFilldeptHospitalId()));
            }
            // 直接插主表数据
            wQbdSoftwareStas.add(softwareSta);
        }
        return super.addList(wQbdSoftwareStas);
    }

    /**
     * 根据主表id查询主表加子表数据
     *
     * @param id 主表id
     * @return
     */
    public WQbdSoftwareSta getParentAndChilden(String id) {
        return dao.getParentAndChilden(id);
    }
}
